package tomcat;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import servlet.LmyCalServlet;
import servlet.LmyHttpServlet;
import tomcat.handler.LmyRequestHandler;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 实现通过xml + 反射来初始化容器
 */
public class LmyTomcatV3 {

    /*1.存放 servletMapping
        ConcurrentHashMap
        key - value
        ServletName 对应的实例
     */
    public static final ConcurrentHashMap<String, LmyHttpServlet> servletMapping
            = new ConcurrentHashMap();

    /*
        2.存放 servletUrlMapping
        ConcurrentHashMap
        key - value
        url-pattern ServletName
     */
    public static final ConcurrentHashMap<String, String> servletUrlMapping
            = new ConcurrentHashMap<>();

    //主程序入口
    public static void main(String[] args) {
        LmyTomcatV3 lmyTomcatV3 = new LmyTomcatV3();
        //初始化
        lmyTomcatV3.init();
        //启动模拟的Tomcat
        lmyTomcatV3.run();
    }

    //直接对两个容器进行初始化
    public void init() {
        //读取web.xml -> dom4j
        //得到web.xml文件的路径
        String path = LmyTomcatV3.class.getResource("/").getPath();
        //使用dom4j完成读取
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(new File(path + "web.xml"));
            //得到根元素
            Element rootElement = document.getRootElement();
            //获取根元素下所有元素
            List<Element> elements = rootElement.elements();
            //遍历并过滤到 servlet servlet-mapping
            for (Element element : elements) {
                if ("servlet".equalsIgnoreCase(element.getName())) {
                    //这是一个servlet配置
                    //使用反射将该servlet实例放到 servletMapping
                    Element servletName = element.element("servlet-name");
                    Element servletClass = element.element("servlet-class");
                    servletMapping.put(servletName.getText(), (LmyCalServlet) Class.forName(servletClass.getText().trim()).newInstance());
                } else if ("servlet-mapping".equalsIgnoreCase(element.getName())) {
                    //这是一个 servlet-mapping配置
                    Element servletName = element.element("servlet-name");
                    Element urlPattern = element.element("url-pattern");
                    servletUrlMapping.put(urlPattern.getText(), servletName.getText());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(servletMapping);
        System.out.println(servletUrlMapping);
    }

    //启动LmyTomcatV3容器
    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(8080);
            System.out.println("=====LmyTomcatV3在8080监听=====");
            while (!serverSocket.isClosed()) {
                Socket socket = serverSocket.accept();
                LmyRequestHandler lmyRequestHandler = new LmyRequestHandler(socket);
                new Thread(lmyRequestHandler).start();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
